package com.example.server.controller.admin;

import com.example.server.common.config.annotation.MustLogin;
import com.example.server.common.config.auth.TokenUser;
import com.example.server.common.constant.Result;
import com.example.server.service.admin.DictService;
import com.example.server.vo.AddDictVO;
import com.example.server.vo.UpdateDictVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

@Api(tags = "Admin字典相关接口")
@RestController
@RequestMapping("/admin/dict")
public class AdminDictController {
    @Resource
    DictService dictService;

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "字典列表", notes = "不分页,字典列表")
    @GetMapping("/list")
    public Result dictList() {
        return new Result(dictService.list());
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "根据父级code获取字典列表", notes = "不分页,字典列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "字典id", name = "pCode", required = true)
    })
    @GetMapping("/list/pCode")
    public Result dictListByPCode(String pCode) {
        return new Result(dictService.dictListByPCode(pCode));
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "根据父级id获取字典列表", notes = "不分页,字典列表")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "字典id", name = "pid", required = true)
    })
    @GetMapping("/list/pid")
    public Result dictListByPid(Integer pid) {
        return new Result(dictService.dictListByPid(pid));
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "新增字典", notes = "新增字典")
    @PostMapping("/add")
    public Result addDict(@RequestBody AddDictVO addDictVO, @ApiIgnore TokenUser tokenUser) {
        return dictService.addDict(addDictVO, tokenUser.getCustomerId());
    }

    @MustLogin(isAdmin = true)
    @ApiOperation(value = "修改字典", notes = "修改字典")
    @PostMapping("/update")
    public Result updateDict(@RequestBody UpdateDictVO updateDictVO, @ApiIgnore TokenUser tokenUser) {
        return dictService.updateDict(updateDictVO, tokenUser.getCustomerId());
    }


    @MustLogin(isAdmin = true)
    @ApiOperation(value = "修改字典", notes = "修改字典")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "字典id", name = "dictId", required = true)
    })
    @PostMapping("/delete")
    public Result deleteDict(Integer dictId) {
        return dictService.deleteDict(dictId);
    }


    @MustLogin(isAdmin = true)
    @ApiOperation(value = "启/禁用字典", notes = "启/禁用字典")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "字典ID", name = "dictId", required = true),
            @ApiImplicitParam(value = "启用/禁用状态", name = "enable", required = true)
    })
    @PostMapping("/setEnable")
    public Result setEnable(Integer dictId, Integer enable, @ApiIgnore TokenUser tokenUser) {
        return dictService.setEnable(dictId, enable, tokenUser.getCustomerId());
    }
}
